const formMake = {
  state: {
    selectWidget: null,
    widgetTypes: {
      number: [], // 表单的所有数值字段
      user: [], // 表单的所有用户字段
      subform: [], // 子表单字段
      linkForm: []
    },
    fieldPermissions: [], // 不可读组件
    roleGroups: [], // 角色组
    BPList: [], // BP类型
    allUserFields: [], // 所有非子表单人员选择组件
    allDeptFields: [] // 所有部门组件
  },
  mutations: {
    SET_SELECT_WIDGET: (state, widget) => {
      state.selectWidget = widget;
    },
    SET_ALL_WIDGET_TYPES: (state, widget) => {
      state.widgetTypes = widget;
    },
    SET_FIELD_PERMISSIONS: (state, data) => {
      state.fieldPermissions = data;
    },
    SET_ROLE_GROUPS: (state, data) => {
      state.roleGroups = data;
    },
    SET_BP_LIST: (state, data) => {
      state.BPList = data;
    },
    SET_ALL_USER_FIELDS: (state, data) => {
      state.allUserFields = data;
    },
    SET_ALL_DEPT_FIELDS: (state, data) => {
      state.allDeptFields = data;
    }
  },
  actions: {
    setSelectWidget({ commit }, widget) {
      commit('SET_SELECT_WIDGET', widget);
    },
    setFieldPermissions({ commit }, data = []) {
      commit('SET_FIELD_PERMISSIONS', data);
    },
    setRoleGroups({ commit }, data = []) {
      commit('SET_ROLE_GROUPS', data);
    },
    setBPList({ commit }, data = []) {
      commit('SET_BP_LIST', data);
    },
    setAllUserFields({ commit }, data = []) {
      commit('SET_ALL_USER_FIELDS', data);
    },
    setAllDeptFields({ commit }, data = []) {
      commit('SET_ALL_DEPT_FIELDS', data);
    },
    initAllWidget({ commit }, widget) {
      if (!widget) {
        commit('SET_ALL_WIDGET_TYPES', {});
        return;
      }
      commit('SET_ALL_WIDGET_TYPES', widget);
    },
    addWidget({ commit, state }, data) {
      const widgetTypes = _.cloneDeep(state.widgetTypes);
      const type = data.type;
      const widget = data.widget;
      if (!type || !widget) return;
      if (!widgetTypes[type]) widgetTypes[type] = [];
      const lists = widgetTypes[type] || [];
      const isRepeat = lists.some(i => i.key === widget.key);
      if (!isRepeat) {
        widgetTypes[type].push(widget);
      }
      commit('SET_ALL_WIDGET_TYPES', widgetTypes);
    },
    removeWidget({ commit, state }, data) {
      const widgetTypes = _.cloneDeep(state.widgetTypes);
      const type = data.type;
      const widget = data.widget;
      if (!type || !widget || !widgetTypes[type]) return;
      widgetTypes[type] = widgetTypes[type].filter(i => {
        return i.key !== widget.key;
      });
      commit('SET_ALL_WIDGET_TYPES', widgetTypes);
    },
    // 名称修改
    changeNameWidget({ commit, state }, data) {
      const widgetTypes = _.cloneDeep(state.widgetTypes);
      const type = data.type;
      const widget = data.widget;
      if (!type || !widget || !widgetTypes[type]) return;
      widgetTypes[type].forEach(i => {
        if (i.key === widget.key) {
          i.name = widget.name;
        }
      });
      commit('SET_ALL_WIDGET_TYPES', widgetTypes);
    },
    // 子表单里面的字段新增或删除
    changeSubform({ commit, state }, data) {
      const widgetTypes = _.cloneDeep(state.widgetTypes);
      const widget = data.widget;
      const formOptions = data.formOptions;
      if (!widget) return;
      const sunformKey = widget.linkFormKey;
      const subformItem = formOptions && formOptions.list.find(i => i.key === sunformKey);
      if (widgetTypes && subformItem && widgetTypes.subform) {
        widgetTypes.subform.forEach(i => {
          if (i.key === sunformKey) {
            i.subform = subformItem.subform;
          }
        });
      }
      commit('SET_ALL_WIDGET_TYPES', widgetTypes);
    },
    changeLinkForm({ commit, state }, data) {
      const widgetTypes = _.cloneDeep(state.widgetTypes);
      const widget = data.widget;
      widgetTypes.linkForm.forEach(i => {
        if (i.key === widget.key) {
          Object.assign(i, widget);
        }
      });
      commit('SET_ALL_WIDGET_TYPES', widgetTypes);
    }
  }
};

export default formMake;
